import {
  defaultadd,
  addBidOrder,
  onePurchase
} from '../../../../api.js'
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_iPhoneX:false,
    ord:-1,
    leave:'',
    defaultadd: '',
    product:[],
    tole:'',
    guojia:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    options.ord?this.setData({
      ord:options.ord
    }):""
    app.globalData.is_iPhoneX ? this.setData({
      is_iPhoneX:true
    }) : ""
    let pro = decodeURIComponent(options.product)
    let prod = pro.replace(/\s*/g, "")
    let product = JSON.parse(prod)
    var that = this
    var price = 0
    for (var i = 0; i < product.length;i++){
      if (product[i].business_country != '中国'){
        that.data.guojia = true
      }
      price = parseInt((price + Number(product[i].nest_price))*100)/100
    }
    this.setData({
      product: product,
      tole: price
    })
    defaultadd({
      token: app.globalData.token
    }).then(res => {
      if (res.data.code == 666) {
        console.log(res.data.data)
        if (res.data.data.length == 0){
          that.setData({
            defaultadd: ''
          })
        }else{
          that.setData({
            defaultadd: res.data.data[0]
          })
        }
      } else if (res.data.code == 111111) {
        wx.showToast({
          title: '登录失效，请重新登录',
          icon: 'none',
        })
        setTimeout(function () {
          wx.reLaunch({
            url: '/pages/user/login/login',
          })
        }, 1500)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  addChoice: function() {
    wx.navigateTo({
      url: '/pages/user/setUp/myadd/myadd?addChoice=0',
    })
  },
  leave:function(e){
    this.setData({
      leave: e.detail.value
    })
  },
  btn_nav: function() {
    var that = this;
    if (that.data.defaultadd == '') {
      wx.showToast({
        title: '请选择地址',
        icon:'none'
      })
      return;
    } else {
      if(this.data.ord==1){
        var product_ids = []
        for (var i = 0; i < that.data.product.length; i++) {
          product_ids.push(that.data.product[i].id)
        }
        var delivery_id = that.data.defaultadd.id;
        var total_price = that.data.tole;
        var leave = that.data.leave;
        onePurchase({
          token: app.globalData.token,
          product_ids: product_ids,
          delivery_id: delivery_id,
          total_price: total_price,
          remark: leave
        }).then(res => {
          if (res.data.code == 666) {
            wx.redirectTo({
              url: '/pages/payment/payment?order=' + res.data.data.batchcode + '&type=1',
            })
          } else if (res.data.code == 111111) {
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none',
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/login/login',
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        })
      }else{
        var product_ids = []
        for (var i = 0; i < that.data.product.length; i++) {
          product_ids.push(that.data.product[i].id)
        }
        var delivery_id = that.data.defaultadd.id;
        var total_price = that.data.tole;
        var leave = that.data.leave;
        addBidOrder({
          token: app.globalData.token,
          product_ids: product_ids,
          delivery_id: delivery_id,
          total_price: total_price,
          remark: leave
        }).then(res => {
          if (res.data.code == 666) {
            wx.redirectTo({
              url: '/pages/payment/payment?order=' + res.data.data.batchcode + '&type=1',
            })
          } else if (res.data.code == 111111) {
            wx.showToast({
              title: '登录失效，请重新登录',
              icon: 'none',
            })
            setTimeout(function () {
              wx.reLaunch({
                url: '/pages/user/login/login',
              })
            }, 1500)
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: 'none'
            })
          }
        })
      }
      
    }
  }
})